package com.shaoming.file;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @Auther: shaoming
 * @Date: 2020/12/30 10:10
 * @Description:
 */
public class FileUtils {
    /**
     *
     * @param filePath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 1.FileWriter的使用
     */
    public static void fileWriterMethod(String filePath, String content) throws IOException {
        try (FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.append(content);
        }
    }

    /**
     *
     * @param filePath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 2. BufferedWriter的使用
     */
    public static void bufferedWriterMethod(String filePath , String content) throws IOException {
        try( BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))){
           bufferedWriter.write(content);
       }
    }

    /**
     *
     * @param filePath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 3.printWriter的使用
     */
    public static void printWriterMethod(String filePath,String content) throws IOException {
        try(PrintWriter printWriter = new PrintWriter(new FileWriter(filePath))){
            printWriter.println(content);
        }
    }

    /**
     * @param filePath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 4.FileOutputStream的使用
     */
    public static void fileOutPutStreamMethod(String filePath , String content) throws IOException {
        try( FileOutputStream fileOutputStream = new FileOutputStream(filePath)){
             byte[] bytes = content.getBytes();
             fileOutputStream.write(bytes);
        }
    }


    /**
     * @param filepath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 5.BufferedOutputStream的使用
     */
    public static void bufferedOutputStreamMethod(String filepath, String content) throws IOException {
        try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(filepath))) {
            bufferedOutputStream.write(content.getBytes());
        }
    }
    /**
     * @param filePath 文件目录
     * @param content  待写入内容
     * @throws IOException
     * 6.files的使用
     */
    public static void filesMethod(String filePath , String content) throws IOException {
        Files.write(Paths.get(filePath),content.getBytes());
    }

}
